import Vue from 'vue'
import VueRouter from 'vue-router'
// import axios from "axios";
import { qtoken } from '@/config/api'

Vue.use(VueRouter);

// login登录页 
const login = () => import("@/views/login/login")


//登录后的小区列表页
const communityList = () => import('@/views/Home/communityList')



//Home  首页  进行页面内容切换
const Home = () => import("@/views/Home/Home")





//导入模块化router
import routeWorkbench from "@/router/moudles/routeWorkbench"
import { routeDataManagement, routeDataManagementChildren } from "@/router/moudles/routeDataManagement"
import { routeDealerShipManagement, routeDealerShipManagementChildren } from "@/router/moudles/routeDealerShipManagement"
import routeEquipmentManagement from "@/router/moudles/routeEquipmentManagement"
import routePayCostManagement from "@/router/moudles/routePayCostManagement"
import routeRealtyService from "@/router/moudles/routeRealtyService"
import routeVisualizationData from "@/router/moudles/routerVisualizationData"
import { routeSystemSettings, messagenotificationDetails } from "@/router/moudles/routeSystemSettings"




export const menu = [
    ...routeWorkbench,
    ...routeDataManagement,
    ...routeDealerShipManagement,
    ...routeRealtyService,
    ...routePayCostManagement,
    ...routeEquipmentManagement,

    ...routeSystemSettings,
    ...routeVisualizationData,

]

let homeRoutes = [
    ...messagenotificationDetails,
    ...routeDealerShipManagementChildren,
    ...routeDataManagementChildren
    // {
    //     title:"报修详情",
    //     path:'Details',
    //     component:Details
    // }


]
menu.forEach(item => {
    homeRoutes = [...homeRoutes, ...item.children]
})

// console.log(123, homeRoutes, menu)

//路由规则
const routes = [
    // {
    //     // 重定向
    //     path: "/",
    //     redirect: "/login"
    // },
    {
        // 登录页面
        path: "/login",
        component: login,
        name: "login",
        meta: {
            requireAuth: false
        }
    },
    {
        // 选择小区
        path: "/communitylist",
        component: communityList,
        name: "communitylist",
        meta: {
            requireAuth: true
        }
    },
    {

        path: "/",
        component: Home,
        name: "home",
        children: homeRoutes,
        readirect: "/workbench",
        meta: {
            requireAuth: true
        }
    },
    // {
    //     path: '*',
    //     redirect: '/404'
    // },
    // {
    //     path: '/error',
    //     name: 'error',
    //     component: () => import('@/views/404.vue'),
    //     meta: {
    //         title: '页面不存在',
    //     }
    // }


]

let router = new VueRouter({
    routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

//路由拦截
router.beforeEach((to, from, next) => {

    //获取token
    const currentToken = window.localStorage.getItem('token');
    let token = currentToken;//获取验证结果

    //拦截目标路由 是否需要登录
    if (to.meta.requireAuth) {


        //如果token存在，放行
        if (token) {
            next();
        } else {
            //否则去往登录页
            next('/login');
        }

    } else {//不需要授权的路由直接放行

        //如果token存在，并且去往的是登录页
        if (token && to.path == '/login') {
            console.log(router.currentRoute);//当前路由currentRoute
            next('/communitylist');//需要重定向到首页
            //next(from.path);//需要重定向到离开页面
        } else {
            next();
        }
    }








    //     //拦截目标路由 是否需要登录
    // if (to.meta.isAuth) {
    //     //如果token存在，放行
    //     if (token) {
    //         next();
    //     } else {
    //         //否则去往登录页
    //         next('/login');
    //     }

    // } else {//不需要授权的路由直接放行

    //     //如果token存在，并且去往的是登录页
    //       if (token && to.path == '/login') {
    //         console.log(router.currentRoute);//当前路由currentRoute
    //         next('/index');//需要重定向到首页
    //         //next(from.path);//需要重定向到离开页面
    //     } else {
    //         next();
    //     }
    // }

});

/* 
//全局后置守卫
router.afterEach((to, from) => {
    // ...
}) */

export default router